"""
脚本用于批量转换现有图像为WebP格式并压缩
"""
import os
import django
import logging
from pathlib import Path

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'food_ordering_system.settings')
django.setup()

from django.conf import settings
from utils.image_utils import ImageProcessor

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler('image_conversion.log'),
    ]
)
logger = logging.getLogger(__name__)

def main():
    """
    主函数
    """
    # 媒体目录路径
    media_root = settings.MEDIA_ROOT
    logger.info(f"开始处理媒体目录: {media_root}")
    
    # 处理食品图片
    foods_dir = os.path.join(media_root, 'foods')
    if os.path.exists(foods_dir):
        logger.info(f"处理食品图片目录: {foods_dir}")
        ImageProcessor.batch_convert_directory(foods_dir, target_size_kb=50)
    else:
        logger.warning(f"食品图片目录不存在: {foods_dir}")
    
    # 处理分类图标
    categories_dir = os.path.join(media_root, 'categories')
    if os.path.exists(categories_dir):
        logger.info(f"处理分类图标目录: {categories_dir}")
        ImageProcessor.batch_convert_directory(categories_dir, target_size_kb=50)
    else:
        logger.warning(f"分类图标目录不存在: {categories_dir}")
    
    logger.info("转换完成!")

if __name__ == "__main__":
    main() 